package com.wzdigit.wms.wms.service.basic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.basic.ZoneAttr;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListZoneAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListZoneAttrRequest.CreateUpdateZoneAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneAttrResponse;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.basic.mapper.core.basic.ZoneAttrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库区-属性 服务类
 * @author yangdongjie
 * @ZoneAttrService.java
 * @2021年1月4日上午11:16:31
 */
@Service
public class ZoneAttrService {

    @Autowired
    private ZoneAttrMapper zoneAttrMapper;

    public <T> PagerInfo<List<ZoneAttrResponse>> searchZoneAttr(
    		Integer zoneId, Pager<T> pager) {
    	Page<T> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<ZoneAttrResponse>> pagerInfo = new PagerInfo<List<ZoneAttrResponse>>();
        List<ZoneAttrResponse> list = zoneAttrMapper.selectAllByZoneId(zoneId);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> createUpdateListZoneAttr(CreateUpdateListZoneAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Date date = new Date();
    	
    	// 记录不删除的属性ID
    	List<Integer> notDelIds = new ArrayList<Integer>();
    	
    	List<CreateUpdateZoneAttrRequest> zoneAttrs = dto.getZoneAttrs();
    	
    	// 去重后的大小
    	Long count = zoneAttrs.stream().map(one -> one.getAttr()).distinct().count();
    	if(count.intValue() != zoneAttrs.size()) {
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	
    	ZoneAttr zoneAttr = new ZoneAttr();
    	zoneAttr.setZoneId(dto.getZoneId());
		for (CreateUpdateZoneAttrRequest one : zoneAttrs) {
			zoneAttr.setAttr(one.getAttr());
			zoneAttr.setValue(one.getValue());
			zoneAttr.setComments(one.getComments());
			zoneAttr.setIsEnabled(one.getIsEnabled());
			if (one.getZoneAttrId() == null) {
				zoneAttr.setCreateUser(loginName);
				zoneAttr.setCreateTime(date);
				zoneAttrMapper.insert(zoneAttr);
			} else {
				zoneAttr.setZoneAttrId(one.getZoneAttrId());
				zoneAttr.setModifyUser(loginName);
				zoneAttr.setModifyTime(date);
				zoneAttrMapper.updateById(zoneAttr);
			}
			notDelIds.add(zoneAttr.getZoneAttrId());
		}
		
		// 删除属性
		if (notDelIds.size() > 0) {
			zoneAttrMapper.deleteByZoneIdAndNotId(dto.getZoneId(), notDelIds);
		} else {
			zoneAttrMapper.deleteByZoneId(dto.getZoneId());
		}
    	return new Result<>();
    }
    
    /*@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneAttrResponse createZoneAttr(CreateZoneAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneAttr zoneAttr = zoneAttrMapper.selectOneByAttrAndZoneId(dto.getAttr(), dto.getZoneId());
    	if(zoneAttr != null) {
    		// 属性已存在
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	zoneAttr = new ZoneAttr();
    	BeanUtil.copyNonNull(dto, zoneAttr);
    	zoneAttr.setCreateUser(loginName);
    	zoneAttr.setCreateTime(new Date());
    	zoneAttrMapper.insert(zoneAttr);
    	
    	ZoneAttrResponse response = new ZoneAttrResponse();
    	BeanUtil.copyNonNull(zoneAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneAttrResponse updateZoneAttr(UpdateZoneAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneAttr zoneAttr = zoneAttrMapper.selectOneByAttrAndZoneId(dto.getAttr(), dto.getZoneId());
    	if(zoneAttr != null) {
    		if (dto.getZoneAttrId().intValue() != zoneAttr.getZoneAttrId().intValue()) {
    			// 属性已存在
    			Shift.fatal(StatusCode.ATTR_DATA_EXIST);
			}
    	}
    	zoneAttr = zoneAttrMapper.selectByPrimaryKey(dto.getZoneAttrId());
    	BeanUtil.copy(dto, zoneAttr);
    	zoneAttr.setModifyUser(loginName);
    	zoneAttr.setModifyTime(new Date());
    	zoneAttrMapper.updateById(zoneAttr);
    	
    	ZoneAttrResponse response = new ZoneAttrResponse();
    	BeanUtil.copy(zoneAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> deleteZoneAttr(Integer whsAttrId) {
    	zoneAttrMapper.deleteByPrimaryKey(whsAttrId);
		return new Result<>();
    }*/
}
